package com.yssoft.service.supervise;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.inspection.TInspReportAtt;
import com.yssoft.mapper.inspection.TInspReportAttMapper;
import com.yssoft.mapper.inspection.TInspReportDetailMapper;
import com.yssoft.mapper.inspection.TInspReportMapper;
import com.yssoft.mapper.production.TCollarMaterialDetailMapper;
import com.yssoft.mapper.production.TCollarMaterialMapper;
import com.yssoft.mapper.production.TWorksheetMapper;
import com.yssoft.service.production.WorkSheetService;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.annotation.WebFilter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人: 陈翼东
 * @时间: 2018/6/11 0011 上午 10:58
 * @说明:
 */
@Service
public class ProductionDetectionService {
    
    /**
     * 加工单
     */
    @Resource
    private TWorksheetMapper worksheetMapper;
    /**
     * 领料单
     */
    @Resource
    private TCollarMaterialMapper collarMaterialMapper;
    /**
     * 领料单明细
     */
    @Resource
    private TCollarMaterialDetailMapper collarMaterialDetailMapper;
    
    /**
     * 检验报告mapper
     */
    @Resource
    private TInspReportMapper inspReportMapper;
    
    @Resource
    private TInspReportDetailMapper inspReportDetailMapper;
    
    @Resource
    private TInspReportAttMapper inspReportAttMapper;
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 查询加工单信息(监管方查看的不分企业)
     * @param page
     * @param limit
     * @param params 条件查询参数
     * @return
     */
    public JSONObject queryWorksheetPage(int page, int limit, Map<String, Object> params) {
        PageHelper.startPage(page,limit);
        List<Map<String,Object>> pageList=worksheetMapper.selectByPage(params);
        List<Map<String,Object>> pageCount=worksheetMapper.selectByPage(params);
       return LayuiUtil.getJSONObjectByMap(pageList,pageCount.size(),"查询成功","0");
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/12 0011 上午 10:26
     * @说明: 根据加工单查询生产工艺指标(监管方查看的不分企业)
     * @return
     */
    public List<Map<String, Object>> selectProductionProcessList(String workId) {
        return worksheetMapper.selectProductionProcessList(workId);
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/12 0011 上午 10:26
     * @说明: 根据加工单查询领料信息(监管方查看的不分企业)
     * @return
     */
    public List<Map<String, Object>> findCollarMaterial(String workId) {
        List<Map<String, Object>> collarMaterialList = collarMaterialMapper.selectCollarMaterialByWorksheet(workId);
        for(Map<String, Object> cmEtnity : collarMaterialList){
            List<Map<String, Object>> detailList =
                    collarMaterialDetailMapper.selectCollarMaterialDetailLsit(Integer.parseInt(cmEtnity.get("id").toString()));
            cmEtnity.put("detailList", detailList);
        }
        return collarMaterialList;
    }
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/12 0011 上午 10:26
     * @说明: 根据加工单查询检验报告(监管方查看的不分企业)
     * @return
     */
    public  List<Map<String,Object>> selectInspectionReport(String workId) {
        List<Map<String,Object>> inspectionList=inspReportMapper.selectByWorksheet(workId);
        for(Map<String,Object> report: inspectionList){
            /*查询检验报告详细*/
            Map<String, Object> param = new HashMap<>();
            param.put("tInspReportId", report.get("id"));
            param.put("deleteFlag", "0");
            System.out.println( report.get("id"));
            List<Map<String,Object>> detailList = inspReportDetailMapper.queryInspReportDetail(param);
            report.put("tInspReportDetailList", detailList);
            /*查询检验报告附件*/
            TInspReportAtt tInspReportAtt = new TInspReportAtt();
            tInspReportAtt.settInspReportId(Integer.parseInt(report.get("id").toString()));
            tInspReportAtt.setDeleteFlag("0");
            List<TInspReportAtt> attList = inspReportAttMapper.select(tInspReportAtt);
            report.put("tInspReportAttList", attList);
        }
        return inspectionList;
    }

	public Map<String, Object> queryDetectionDetail(Integer worksheetId) {
		Map<String, Object> params=new HashMap<>();
		params.put("worksheetId", worksheetId);
		List<Map<String,Object>> pageList=worksheetMapper.selectByPage(params);
		if(pageList!=null && pageList.size()>0) {
			params=pageList.get(0);
		}
		return params;
	}
}
